package com.itheima.mybatis.framework;

import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2020/6/29
 * @description ：SqlSession默认实现
 * @version: 1.0
 */
public class DefaultHmSqlSession implements HmSqlSession {


	@Override
	public <T> List<T> selectList(String mapperId) {
		return null;
	}

	@Override
  public <T> T getMapper(Class<T> type) {
    Class clazz[] = new Class[]{type};
    Object  obj = Proxy.newProxyInstance(this.getClass().getClassLoader(), clazz,
      // 动态实现
      new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        // 动态实现方法内容
        Select select = method.getAnnotation(Select.class);
        if(select != null){
        	String sql = select.value()[0];
        	System.out.println("args:"+Arrays.toString(args));
        	System.out.println("execute sql："+sql);
		}
        // 构建方法返回值对象
		  Object obj = method.getReturnType().newInstance();
        // 动态方法的返回值
        return obj;
      }
    });
    return (T)obj;
  }

  @Override
  public void close() {

  }

  @Override
  public void commit() {

  }
}
